import java.util.ArrayList;

/*
题目描述：二叉树中和为某一值的路径
输入一个二叉树和一个整数，输出二叉树结点值的和等于该整数的所有路径
方法：
回溯
 */
public class E34 {
    public static void main(String[] args) {
        TreeNode1 root = new TreeNode1(1);
        root.left = new TreeNode1(2);
        root.right = new TreeNode1(3);
        root.left.left = new TreeNode1(4);
        root.left.right = new TreeNode1(5);
        root.right.left = new TreeNode1(6);
        root.right.right = new TreeNode1(7);

        ArrayList<ArrayList<Integer>> res1 = FindPath(root, 7);

        System.out.println(res1);
    }

    static ArrayList<ArrayList<Integer>> res = new ArrayList<>();
    static ArrayList<Integer> cur = new ArrayList<>();
    public static ArrayList<ArrayList<Integer>> FindPath(TreeNode1 root, int target) {
        if(root == null){
            return res;
        }
        cur.add(root.val);
        target -= root.val;
        if(target == 0 && root.left == null && root.right == null){
            res.add(new ArrayList<>(cur));//这里要new一个对象进去，不然都是空的！
        }
        FindPath(root.left, target);
        FindPath(root.right, target);
        cur.remove(cur.size() - 1);
        return res;
    }
}